import QtQuick 1.1
import QtWebKit 1.0
import Qt 4.7
import "js/dbmanager.js" as DB
import "js/snr-app.js" as APP
import "ui/"

Item {
    property bool debug : false
    property string snapUrl: "http://sna.pr";
    property string textLoggedIn : "Congratulations, you are logged in";
    property string textLoggedOut : "You was loggged out from application";

    SnaprAuthentification{
        id: snaprAuhtentification;


        onStateChanged: {

            if(debug) console.log("state: " + state);
            if(snaprAuhtentification.state==DB.STATES.AUTHDONE){
                logoutButton.visible = true;
                timer.start();
            }
            if(snaprAuhtentification.state==DB.STATES.LOGIN){
                if(debug) {
                    console.log("disable logoutButton");
                }
                logoutButton.visible = false;
            }
        }
    }

    states: [
        State {
            name: DB.STATES.LOGIN
            PropertyChanges { target: logoutButton; visible: false; }
        },
        State {
            name: DB.STATES.AUTHDONE
            PropertyChanges { target: webViewId; visible: true; }
            PropertyChanges { target: notifications; visible: true; text: textLoggedIn; state: "visible";}
            PropertyChanges { target: timer; startTimer: true;}
            PropertyChanges { target: logoutButton; visible: true;  }
        }
    ]

    WebView {
        id: webViewId
        visible: false
        url: snapUrl
        anchors.fill: parent
    }

    function showNotification(text) {
        notifications.text = text;
        notifications.show();
    }

    Timer {

        id: timer
        property  bool startTimer: false;
        onStartTimerChanged: {
            if(startTimer) timer.start();
        }

        interval: APP.timerInterval;
        repeat: false
        onTriggered: {
            notifications.visible = false;
        }
    }

    Notification {
        id: notifications
    }

    Button {
        id: logoutButton
        buttonImage: APP.getProjectImage("button_middleBlack_btn.png");
        buttonImagePressed: APP.getProjectImage("button_middleBlack_pressed_btn.png");
        buttonText: "Logout"
        buttonTextColor: "#FFFFFF"
        width: APP.buttonWidth * APP.scale
        height: APP.buttonHeight * APP.scale
        visible: false
        anchors{
            bottom: quitButton.top
            bottomMargin: APP.halfMargin
            left: parent.left;
            leftMargin: APP.halfMargin;
        }

        onClicked: {
            snaprAuhtentification.state=DB.STATES.LOGIN;
            logoutButton.visible=false;
            DB.deleteToken();
            showNotification(textLoggedOut);

        }
    }

    Button {
        id: quitButton
        buttonImage: APP.getProjectImage("button_middleBlack_btn.png");
        buttonImagePressed: APP.getProjectImage("button_middleBlack_pressed_btn.png");
        buttonText: "Quit"
        buttonTextColor: "#FFFFFF"
        width: APP.buttonWidth * APP.scale
        height: APP.buttonHeight * APP.scale

        anchors{
            bottom: parent.bottom
            bottomMargin: 2*APP.margin + APP.halfMargin
            left: parent.left;
            leftMargin: APP.halfMargin;
        }

        onClicked: {
            Qt.quit();
        }
    }



}

